package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.rare;

import net.minecraft.block.Blocks;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

public class WeavingWebSkill extends BaseSkill {

    public WeavingWebSkill(int level) {
        super(level);
    }

    public WeavingWebSkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public WeavingWebSkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return String.format("织网技能，在玩家脚底下生成蜘蛛网。");
    }

    public SkillRarity getSkillRarity() {
        return SkillRarity.Rare;
    }

    public boolean canUseAbility(MobEntity mobEntity) {
        return (mobEntity.getTarget() != null && getSkilltime() < mobEntity.world.getTime());
    }

    @Override
    public boolean onAbilityUse(MobEntity mobEntity) {
        if (canUseAbility(mobEntity)) {
            setSkilltime(mobEntity.world.getTime() + getCooldown() - getSkillLevel() * 5L);
            LivingEntity target = mobEntity.getTarget();
            int X = MathHelper.floor(target.getX() * 1.0);
            int Y = MathHelper.floor(target.getY() * 1.0);
            int Z = MathHelper.floor(target.getZ() * 1.0);
            BlockPos blockPos = new BlockPos(X, Y, Z);
            mobEntity.world.setBlockState(blockPos, Blocks.COBWEB.getDefaultState(), 3);
        }
        return true;
    }
}
